﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models.Interact;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels.Dialogs
{
    public class NewInteractAddressViewDialog : Domains.ViewModelBase
    {
        private bool _isUpdate = false;
        private System.Windows.Visibility _sUpdate;

        public System.Windows.Visibility IsUpdate
        {
            get { return _sUpdate; }
            set { SetProperty(ref _sUpdate, value); }
        }

        private DeviceConfigStatic _deviceConfig;
        private SystemConfigStatic _configStatic;
        public InteractAddressModel IAM { get; set; }
        public NewInteractAddressViewDialog(SystemConfigStatic configStatic, DeviceConfigStatic deviceConfig, InteractAddressModel model, bool isUpdate = false)
        {
            _deviceConfig = deviceConfig;
            _configStatic = configStatic;
            _isUpdate = isUpdate;
            ServiceList = new ObservableCollection<string>();
            IndexList = new ObservableCollection<int>();
            DeviceIndexList = new ObservableCollection<int>();
            foreach (var item in configStatic.SignalList)
            {
                ServiceList.Add(item.Name);
            }
            if (_isUpdate)
            {
                IAM = model;
                IsUpdate = System.Windows.Visibility.Visible;
                ServiceName = IAM.ServiceName;
                DeviceType = IAM.DeviceType;
            }
            else
            {
                IsUpdate = System.Windows.Visibility.Collapsed;
                IAM = new InteractAddressModel();
            }
        }

        public Commands.DelegateCommand Confirm => new Commands.DelegateCommand(() =>
        {
            if (IAM.Index == 0)
            {
                if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
                return;
            }
            if (!_isUpdate)
            {
                Guid guid = Guid.NewGuid();
                _configStatic.IAddressList.Add(new InteractAddressModel() {
                    Label = IAM.Label,
                    Index = IAM.Index,
                    ServiceName = IAM.ServiceName,
                    DeviceIndex = IAM.DeviceIndex,
                    Struct_Length = IAM.Struct_Length,
                    //Address_Complete = IAM.Address_Complete,
                    Address_Struct = IAM.Address_Struct,
                    DeviceType = IAM.DeviceType,
                    TaskType = IAM.TaskType,
                    ThisGuid = guid,
                });
            }
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

        public Commands.DelegateCommand Delete => new Commands.DelegateCommand(() =>
        {
            _configStatic.IAddressList.Remove(IAM);
            _configStatic.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

        #region 属性
        private string _ServiceName;
        /// <summary>
        /// 服务名
        /// </summary>
        public string ServiceName
        {
            get { return _ServiceName; }
            set
            {
                IndexList.Clear();
                if (value != null)
                {
                    var model = _configStatic.SignalList.FirstOrDefault(x => x.Name == value);
                    if (model != null)
                    {
                        for (int i = 1; i < (model.Length_Signal + 1); i++)
                        {
                            if (_configStatic.IAddressList.Any(x => x.ServiceName == value && x.Index == i))
                            {
                                continue;
                            }
                            IndexList.Add(i);
                        }
                    }
                }
                _ServiceName = value;
                IAM.ServiceName = value;
            }
        }
        public ObservableCollection<string> ServiceList { get; set; }
        public ObservableCollection<int> DeviceIndexList { get; set; }
        public ObservableCollection<int> IndexList { get; set; }
        public IEnumerable<TaskTypes> TaskList
        {
            get
            {
                foreach (TaskTypes c in (TaskTypes[])Enum.GetValues(typeof(TaskTypes)))
                {
                    if (c != TaskTypes.扫描信号)
                    {
                        yield return c;
                    }
                }
            }
        }
        public IEnumerable<DeviceTypes> DeviceList
        {
            get
            {
                foreach (DeviceTypes c in (DeviceTypes[])Enum.GetValues(typeof(DeviceTypes)))
                {
                    yield return c;
                }
            }
        }

        private DeviceTypes _deviceType;

        public DeviceTypes DeviceType
        {
            get { return _deviceType; }
            set
            {
                DeviceIndexList.Clear();
                if (value == DeviceTypes.NONE)
                {
                    for (int i = 1; i < 13; i++)
                    {
                        DeviceIndexList.Add(i);
                    }
                }
                else
                {
                    int length = _deviceConfig.DeviceList.Count(x => x.ServiceName == _ServiceName && x.DeviceType == value) + 1;
                    for (int i = 1; i < length; i++)
                    {
                        //if (_configStatic.IAddressList.Any(x => x.ServiceName == _ServiceName && x.DeviceIndex == i && x.DeviceType == value && x.TaskType == IAM.TaskType))
                        //{
                        //    continue;
                        //}
                        DeviceIndexList.Add(i);
                    }
                }
                _deviceType = value;
                IAM.DeviceType = value;
            }
        }

        #endregion
    }
}
